<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-07-20
 * Time: 上午 9:23
 * 车辆控制器
 */
namespace app\admin\controller;
use app\admin\model\MemberModel;
use app\admin\model\CarModel;
use app\api\controller\Excel;
class Car extends Base{
    //车辆列表
    public function index(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['phone'])) {
                $where['snake_member.member_phone'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['carnum'])) {
                $where['snake_car.car_number'] = $param['carnum'];
            }
            if (!empty($param['name'])) {
                $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['status'])) {
                $where['snake_car.car_insure'] = $param['status'];
            }
            $where['snake_car.car_isdel'] = 1;
            $car = new CarModel();
            $join = "snake_member";
            $condition="snake_car.member_id = snake_member.member_id";

            $selectResult = $car->getCarByWhere($where, $offset, $limit,$join,$condition);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['checked'] = "<input type='checkbox' name='check' value='".$vo['car_id']."'>";
                $selectResult[$key]['car_insure'] = $car->getCarInsure($vo['car_insure']);
                $selectResult[$key]['car_expire'] = date('Y-m-d',$vo['car_expire']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['car_id']));
            }

            $return['total'] = $car->getCarCount($where,$join,$condition);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }
    /***
     * 删除车辆信息
     */
    public function carDel(){
        $car = new CarModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }
        if($param['type'] == 1){
            $map['car_id'] = $param['id'];
        }else{
            $map= " car_id in (".implode(',',explode('8',$param['id'])).")";
        }
        $data['car_isdel'] = 0;
        $res = $car->getCarUpdate($map,$data);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }

    /**
     * 导出车辆记录
     */
    public function out(){
        $param = input('param.');
        $where = [];
        if (!empty($param['phone'])) {
            $where['snake_member.member_phone'] = ['like', '%' . $param['phone'] . '%'];
        }
        if (!empty($param['carnum'])) {
            $where['snake_car.car_number'] = $param['carnum'];
        }
        if (!empty($param['name'])) {
            $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['status'])) {
            $where['snake_car.car_insure'] = $param['status'];
        }
        $where['snake_car.car_isdel'] = 1;
        $car = new CarModel();
        $join = "snake_member";
        $condition="snake_car.member_id = snake_member.member_id";

        //导出
        $data = $car->getCarByWhereData($where,$join,$condition);
        foreach($data as $key=>$vo){
            $data[$key]['car_insure'] = $car->getCarInsure($vo['car_insure']);
            $data[$key]['car_expire'] = date('Y-m-d',$vo['car_expire']);
        }
        $Excel = new Excel();
        $filename = '会员车辆投保情况';
        $title = array('会员名称','会员手机号','车品牌','车系','车牌照','车架号','发动机号','是否可投保','车险到期时间');
        $field = array('member_name','member_mobile','car_brand_name','car_xilie_name','car_number','car_vin','car_engine','car_insure','car_expire');
        $Excel->out($filename,$filename,9,$title,$field,$data);die;

    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'car/cardel',
                'href' => "javascript:carDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}